package cn.ac.jaas.isc.vegetable.pbms.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.ac.jaas.isc.vegetable.common.core.util.R;
import cn.ac.jaas.isc.vegetable.common.log.annotation.SysLog;
import cn.ac.jaas.isc.vegetable.pbms.api.entity.ArcHouse;
import cn.ac.jaas.isc.vegetable.pbms.service.ArcHouseService;
import org.springframework.security.access.prepost.PreAuthorize;
import cn.ac.jaas.isc.vegetable.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 温室表
 *
 * @author 张高昌
 * @date 2022-05-02 09:38:48
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/archouse")
@Api(value = "archouse", tags = "温室管理")
public class ArcHouseController {

    private final ArcHouseService arcHouseService;

    /**
     * 分页查询
     *
     * @param page     分页对象
     * @param arcHouse 温室表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('pbms_archouse_view')")
    public R getArcHousePage(Page page, ArcHouse arcHouse) {
        return R.ok(arcHouseService.page(page, Wrappers.query(arcHouse)));
    }


    /**
     * 通过id查询温室表
     *
     * @param houseId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{houseId}")
    @PreAuthorize("@pms.hasPermission('pbms_archouse_view')")
    public R getById(@PathVariable("houseId") Integer houseId) {
        return R.ok(arcHouseService.getById(houseId));
    }

    /**
     * 新增温室表
     *
     * @param arcHouse 温室表
     * @return R
     */
    @ApiOperation(value = "新增温室表", notes = "新增温室表")
    @SysLog("新增温室表")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('pbms_archouse_add')")
    public R save(@RequestBody @Valid ArcHouse arcHouse) {
        return arcHouseService.saveHouse(arcHouse);
    }

    /**
     * 修改温室表
     *
     * @param arcHouse 温室表
     * @return R
     */
    @ApiOperation(value = "修改温室表", notes = "修改温室表")
    @SysLog("修改温室表")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('pbms_archouse_edit')")
    public R updateById(@RequestBody @Valid ArcHouse arcHouse) {
        if (arcHouse.getHouseId() == null) {
            return R.failed("温室id不能为空");
        }
        return arcHouseService.updateHouseById(arcHouse);
    }

    /**
     * 通过id删除温室表
     *
     * @param houseId id
     * @return R
     */
    @ApiOperation(value = "通过id删除温室表", notes = "通过id删除温室表")
    @SysLog("通过id删除温室表")
    @DeleteMapping("/{houseId}")
    @PreAuthorize("@pms.hasPermission('pbms_archouse_del')")
    public R removeById(@PathVariable Integer houseId) {
        return R.ok(arcHouseService.removeById(houseId));
    }


    /**
     * 导出excel 表格
     *
     * @param arcHouse 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('pbms_archouse_export')")
    public List<ArcHouse> export(ArcHouse arcHouse) {
        return arcHouseService.list(Wrappers.query(arcHouse));
    }

    /**
     * 获取所有温室
     *
     * @return
     */
    @ApiOperation(value = "获取所有温室", notes = "获取所有温室")
    @GetMapping("/getAllHouse")
    @PreAuthorize("@pms.hasPermission('pbms_archouse_view')")
    public R getAllHouse() {
        ArcHouse arcHouse = new ArcHouse();
        arcHouse.setDelFlag(0);
        return R.ok(arcHouseService.list(Wrappers.query(arcHouse)));
    }
}
